package com.greenway.beans.clickstates;

import java.io.IOException;

import javax.faces.application.FacesMessage;

import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.json.JSONException;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

import com.greenway.beans.MapBean;

public class FinishMarkerState implements State {

	private MapBean mapBean;

	public FinishMarkerState(MapBean mapBean) {
		this.mapBean = mapBean;
	}

	@Override
	public void handleMarker(PointSelectEvent event, MapModel mapModel)
			throws IOException, JSONException {

		LatLng latlng = event.getLatLng();

		// Create start marker
		Marker markerFinish = new Marker(latlng, "finish", null,
				"http://localhost:9090/GreenPlanet/images/finish.png");
		markerFinish.setDraggable(true);
		mapModel.addOverlay(markerFinish);

		mapBean.setState(mapBean.getNothingOnMapClickState());

		mapBean.addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Info",
				"Please see your route and shits"));

		mapBean.addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Point Selected", "Lat:" + latlng.getLat() + ", Lng:"
						+ latlng.getLng()));

		mapBean.drawRout();
		mapBean.setPollutions();
	}

}
